Skip to main content
Version: 1.0.16

ALTER SEQUENCE

ALTER SEQUENCE — Change the Definition of a Sequence Generator

Synopsis

ALTER SEQUENCE [ IF EXISTS ] name

[ AS data_type ]

[ INCREMENT [ BY ] increment ]

[ MINVALUE minvalue | NO MINVALUE ] [ MAXVALUE maxvalue | NO MAXVALUE ]

[ START [ WITH ] start ]

[ RESTART [ [ WITH ] restart ] ]

[ CACHE cache ] [ [ NO ] CYCLE ]

[ OWNED BY { table_name.column_name | NONE } ]

ALTER SEQUENCE [ IF EXISTS ] name OWNER TO { new_owner | CURRENT_USER |

SESSION_USER }

ALTER SEQUENCE [ IF EXISTS ] name RENAME TO new_name

ALTER SEQUENCE [ IF EXISTS ] name SET SCHEMA new_schema

Description

ALTER SEQUENCE changes the parameters of an existing sequence generator. Any parameters not explicitly set in the ALTER SEQUENCE command retain their previous settings.

To use ALTER SEQUENCE, you must own the sequence. To change the schema of a sequence, you must also have CREATE privilege on the new schema. To change the owner, you must also be a direct or indirect member of the new owning role, and that role must have CREATE privilege on the sequence's schema (these restrictions enforce that changing the owner cannot do anything you couldn't do by dropping and recreating the sequence. However, a superuser can change ownership of any sequence).

Parameters

name

The name (optionally schema-qualified) of the sequence to be modified.

IF EXISTS

Do not throw an error if the sequence does not exist. A notice is issued in this case.

data_type

The optional clause AS data_type changes the data type of the sequence. Valid types are smallint, integer, and bigint.

Changing the data type automatically changes the minimum and maximum values of the sequence if and only if the previous minimum and maximum values were the minimum or maximum of the old data type (in other words, if the sequence had been created using NO MINVALUE or NO MAXVALUE, either implicitly or explicitly). Otherwise, the minimum and maximum values are preserved, unless new values are given as part of the same command. If the minimum and maximum values do not fit the new data type, an error will be generated.

increment

The clause INCREMENT BY increment is optional. A positive value will produce an ascending sequence, a negative value a descending sequence. If not specified, the previous increment value is retained.

minvalue

NO MINVALUE

The optional clause MINVALUE minvalue determines the minimum value a sequence can generate. If NO MINVALUE is specified, the defaults for ascending and descending sequences are 1 and the minimum value of the data type, respectively. If neither option is specified, the current minimum value is retained.

maxvalue

NO MAXVALUE

The optional clause MAXVALUE maxvalue determines the maximum value a sequence can generate. If NO MAXVALUE is specified, the defaults for ascending and descending sequences are the maximum value of the data type and -1, respectively. If neither option is specified, the current maximum value is retained.

Start

The optional clause START WITH start changes the recorded start value of the sequence. This has no effect on the current sequence value; it simply sets the value that will be used by future ALTER SEQUENCE RESTART commands.

restart

The optional clause RESTART [ WITH restart ] changes the current value of the sequence. This is similar to calling the setval function with is_called = false: the specified value will be returned by the next nextval call. Writing RESTART with no restart value is equivalent to supplying the start value that was recorded by CREATE SEQUENCE or was last set by ALTER SEQUENCE START WITH.

Compared to a setval call, the RESTART operation on a sequence is transactional and blocks concurrent transactions from obtaining numbers from the same sequence. If this is not the desired mode of operation, setval should be used.

cache

The clause CACHE cache enables sequence numbers to be pre-allocated and stored in memory for faster access. The minimum value is 1 (only one value is generated at a time, i.e., no cache). If not specified, the old cache value is retained.

CYCLE

The optional CYCLE keyword may be used to allow the sequence to wrap around when it reaches maxvalue (for ascending sequences) or minvalue (for descending sequences). If the limit is reached, the next number generated will be minvalue or maxvalue, respectively.

NO CYCLE

If the optional NO CYCLE keyword is specified, any nextval call after the sequence reaches its maximum value will return an error. If neither CYCLE nor NO CYCLE is specified, the old cycle behavior is retained.

OWNED BY table_name.column_name

OWNED BY NONE

The OWNED BY option causes the sequence to be associated with a specific table column, so that if that column (or its whole table) is dropped, the sequence will be automatically dropped as well. If specified, this association replaces any previously specified association for the sequence. The specified table must have the same owner and be in the same schema as the sequence. Specifying OWNED BY NONE removes any existing association, making the sequence "standalone".

new_owner

The user name of the new owner of the sequence.

new_name

The new name for the sequence.

new_schema

The new schema for the sequence.

Notes

ALTER SEQUENCE will not immediately affect nextval results in backends other than the current one, because they have pre-allocated (cached) sequence values. They will use up all cached values before noticing the changed sequence generation parameters. The current backend will be affected immediately.

ALTER SEQUENCE does not affect the currval state of the sequence.

ALTER SEQUENCE blocks concurrent nextval, currval, lastval, and setval calls.

For historical reasons, ALTER TABLE can also be used with sequences, but only the ALTER TABLE variants that are equivalent to the forms above are allowed for sequences.

Examples

# Restart a sequence called serial at 105:

ALTER SEQUENCE serial RESTART WITH 105;

See Also

CREATE SEQUENCE, DROP SEQUENCE